pacman::p_load(fixest, tidyverse, broom, data.table, countrycode, kableExtra)

df <-  readRDS("Data/Regimedata/Measures_merged_covariates.rds")

names(df)





# normalize cagenmob and physical violence measure



df$v2cagenmob_nor <- (df$v2cagenmob - min(df$v2cagenmob, na.rm = T)) / (max(df$v2cagenmob, na.rm = T) - min(df$v2cagenmob, na.rm = T))

df$v2x_clphy*-1 
df$v2x_clphy_nor <- (df$v2x_clphy - min(df$v2x_clphy, na.rm = T)) / (max(df$v2x_clphy, na.rm = T) - min(df$v2x_clphy, na.rm = T)) 

summary(df$v2cagenmob_nor)
summary(df$v2x_clphy_nor)


depvars <- c("reg_breakdown", "onset1", "v2x_clphy_nor", "v2cagenmob_nor", "coup_attempt")

names(df)

indepvars <- c("RegType_lied_CA + RegType_lied_DEM",
               "RegType_RoW_CA + RegType_RoW_DEM",
               "Politytype_Autocracy + Politytype_Democracy",
               "status_fh_NF + status_fh_F",
               "HTW_RegType_Non_MP_Autocracy + HTW_RegType_Democracy",
               "RegType_magaloni_CA + RegType_magaloni_DEM",
               "AnckarRegtype_Non_MP_Autocracy + AnckarRegtype_Democracy")



# Create a list of all possible combinations of depvars and indepvars
combinations <- expand.grid(depvars, indepvars)

combinations




df2 <- df %>% 
  select(RegType_lied_EA, RegType_RoW_EA,  Politytype_Anocracy, status_fh_PF,
         HTW_RegType_MP_Autocracy, RegType_magaloni_EA, AnckarRegtype_MP_Autocracy, year, Final_Code) %>% 
  na.omit() %>%
  select(year, Final_Code) %>%
  left_join(df, by = c("year", "Final_Code"))

names(df2)


unique_countries <- unique(df2$Final_Code)

variance_list <- list()
variance_list_ea <- list()
variance_list_hr <- list()
variance_list_mpr <- list()

for (i in 1:length(unique_countries)) {

excluded <- unique_countries[i]
  
results <- apply(combinations, 1, function(x) {
  depvar <- x[1]
  indepvar <- x[2]
  
  list(
    feols(as.formula(paste(depvar, "~", indepvar, " | Final_Code + year")), cluster = "Final_Code", data = df2 %>% filter(Final_Code != excluded)) %>%tidy() %>%
      mutate(FE  = "Country & Year", Sample = "Overlapping Sample", Outcome = depvar)) %>% rbindlist()
  
  
})


coefplotframe <- results %>% 
  rbindlist %>% 
  as.data.frame %>% filter ( term != "(Intercept)" & FE == "Country & Year" & Sample == "Overlapping Sample")

coefplotframe$Regime <- rep(c("Other Autocracy", "Democracy"), nrow(coefplotframe)/2)

ea_frame <- coefplotframe %>% filter(term %in% c("RegType_lied_CA", "RegType_lied_DEM", "RegType_RoW_CA", "RegType_RoW_DEM"))
hr_frame <- coefplotframe %>% filter(term %in% c("status_fh_NF", "status_fh_F","Politytype_Democracy", "Politytype_Autocracy" ))
mpr_frame <- coefplotframe %>% filter(term %in% c("HTW_RegType_Non_MP_Autocracy", "HTW_RegType_Democracy", "RegType_magaloni_CA",
                                                  "RegType_magaloni_DEM", "AnckarRegtype_Non_MP_Autocracy", "AnckarRegtype_Democracy" ))



variance_list[[i]] <- coefplotframe %>% group_by(Outcome, Regime) %>%
  summarize(variance_est = var(estimate),
            variance_stat = var(statistic)) %>%
  mutate(Country_excluded = excluded)



variance_list_ea[[i]] <- ea_frame %>% group_by(Outcome, Regime) %>%
  summarize(variance_est = var(estimate),
            variance_stat = var(statistic)) %>%
  mutate(Country_excluded = excluded)

variance_list_hr[[i]] <- hr_frame %>% group_by(Outcome, Regime) %>%
  summarize(variance_est = var(estimate),
            variance_stat = var(statistic)) %>%
  mutate(Country_excluded = excluded)

variance_list_mpr[[i]] <- mpr_frame %>% group_by(Outcome, Regime) %>%
  summarize(variance_est = var(estimate),
            variance_stat = var(statistic)) %>%
  mutate(Country_excluded = excluded)

}





variance_df <- variance_list %>% 
  rbindlist %>% 
  as.data.frame 





variance_df <- variance_df%>%mutate(country_name = countrycode(Country_excluded, origin = "cown", destination = "country.name"))
variance_df$country_name <- if_else(variance_df$Country_excluded == 260,"Germany", variance_df$country_name)





top_countries <- variance_df %>% select(Outcome, Regime, variance_stat, country_name) %>%
  group_by(Outcome, Regime) %>%
  slice_min(variance_stat, n = 10) %>%
  arrange(Outcome, Regime, variance_stat)


top_countries %>%
  group_by(country_name) %>%
  summarise(count = n()) %>%
  arrange(desc(count)) %>%
  group_by(count) %>%
  summarise(count = n())

 

collapsed_countries <- top_countries %>%
  group_by(Outcome, Regime) %>%
  summarize(countries = paste(country_name, collapse = ", ")) %>%
  arrange(Outcome, Regime)




collapsed_countries$Outcome <- as.factor(collapsed_countries$Outcome)


levels(collapsed_countries$Outcome) <- c("Coup attempt", "Intrastate conflict", "Regime breakdown", "Mass mobilization", "Repression")



collapsed_countries


top_countries2 <- variance_df %>% select(Outcome, Regime, variance_est, country_name) %>%
  group_by(Outcome, Regime) %>%
  slice_min(variance_est, n = 10) %>%
  arrange(Outcome, Regime, variance_est)

collapsed_countries2 <- top_countries2 %>%
  group_by(Outcome, Regime) %>%
  summarize(countries = paste(country_name, collapse = ", ")) %>%
  arrange(Outcome, Regime)


collapsed_countries2$Outcome <- as.factor(collapsed_countries2$Outcome)


levels(collapsed_countries2$Outcome) <- c("Coup attempt", "Intrastate conflict", "Regime breakdown", "Mass mobilization", "Repression")



### Table 4: 
collapsed_countries2
####








###by conceptual group (Table J1)

variance_df_ea <- variance_list_ea %>% 
  rbindlist %>% 
  as.data.frame %>% mutate(concept = "Electoral Autocracy")

variance_df_mpr <- variance_list_mpr %>% 
  rbindlist %>% 
  as.data.frame %>% mutate(concept = "Multiparty Autocracy")

variance_df_hr <- variance_list_hr %>% 
  rbindlist %>% 
  as.data.frame %>% mutate(concept = "Hybrid Regimes")


variance_df_gr <- rbind(variance_df_ea, variance_df_hr, variance_df_mpr)



variance_df_gr <- variance_df_gr%>%mutate(country_name = countrycode(Country_excluded, origin = "cown", destination = "country.name"))
variance_df_gr$country_name <- if_else(variance_df_gr$Country_excluded == 260,"Germany", variance_df_gr$country_name)





top_countries <- variance_df_gr %>% select(Outcome, Regime, variance_stat, country_name, concept) %>%
  group_by(Outcome, concept, Regime) %>%
  slice_min(variance_stat, n = 10) %>%
  arrange(Outcome, Regime,concept, variance_stat)





collapsed_countries <- top_countries %>%
  group_by(Outcome, Regime, concept) %>%
  summarize(countries = paste(country_name, collapse = ", ")) %>%
  arrange(Outcome, Regime, concept)


collapsed_countries$Outcome <- as.factor(collapsed_countries$Outcome)
levels(collapsed_countries$Outcome) <- c("Coup attempt", "Intrastate conflict", "Regime breakdown", "Mass mobilization", "Repression")



### Table J1

collapsed_countries

